<template>
  <div class="breadcrumb">
    <!-- 导航栏部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- 默认样式紧挨着，这里的el-breadcrumb设置在全局样式文件 global.css中 -->
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片主体区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="30" class="mianbaoxie">
        <!-- :gutter="10"设置内部el-col之间的边距 -->
        <el-col :span="8">
          <!-- clearable ：多个可一键清空的小图标     @clear：点击一键清空后触发的事件-->
          <el-input
            placeholder="请输入内容"
            v-model="orderParams.query"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 信息展示部分 -->
      <el-table :data="orderList" border stripe>
        <!-- border：显示边框   stripe：隔行变色 -->
        <el-table-column type="index" lable="#"></el-table-column>
        <el-table-column prop="order_id" label="订单编号"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- size设置按钮大小 -->
          <!-- 通过 template来定义scope，通过其可以获取 row当前行信息，然后.mg_state就是用户状态的参数，设置到 switch 上就能显示状态了 -->
          <template slot-scope="scope">
            <el-button
              class="el-icon-edit"
              type="primary"
              size="mini"
              @click="editDialogVisible = true"
            ></el-button>
            <el-button
              class="el-icon-location"
              type="success"
              size="mini"
              @click="getOrderTranMessage()"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderParams.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="orderParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址的消息框 -->
    <!-- close：dialog表单关闭事件 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialogVisible"
      width="40%"
      @close="editDialogClose"
    >
      <!-- ref：表单提交的对象名   rules：数据规则     model：数据对象，用于保存以便其他功能方法使用 -->
      <el-form
        :model="editAddressForm"
        :rules="addressRules"
        ref="editAddressFormRef"
        label-width="100px"
        class="demo-ruleForm"
        ><!-- el-form 的 :model属性值必须对应下面各个分标签的值 -->
        <el-form-item label="省区市/县" prop="address1">
          <el-cascader
            clearable
            v-model="addressList"
            :options="cityData"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看物流的消息框 -->
    <!-- close：dialog表单关闭事件 -->
    <el-dialog title="查看物流" :visible.sync="orderDialogVisible" width="40%">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in orderTranMessage"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata.js";
export default {
  data() {
    return {
      orderParams: {
        query: "",
        pagenum: 1,
        pagesize: 5,
        user_id: "",
        pay_status: "",
        is_send: "",
        order_fapiao_title: "",
        order_fapiao_company: "",
        order_fapiao_content: "",
        consignee_addr: "",
      },
      orderList: [],
      total: 0,
      // 省区县分级数据
      cityData: cityData,
      // 修改地址的可见属性
      editDialogVisible: false,
      // 修改地址的表单数据
      editAddressForm: {
        address1: [],
        address2: "",
      },
      addressRules: {
        address1: [
          { required: true, message: "请选择省区市县", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" },
        ],
      },
      // 选择的地址数组
      addressList: [],
      // 物流信息显示框显示参数
      orderDialogVisible: false,
      // 物流信息
      orderTranMessage: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.orderParams,
      });
      if (res.meta.status != 200)
        return this.$messgae.error("订单数据返回失败！");
      this.orderList = res.data.goods;
      this.total = res.data.total;
      console.log(res);
    },
    // 改变页码
    handleSizeChange(newSize) {
      this.orderParams.pagesize = newSize;
      this.getOrderList();
    },
    // 改变页数
    handleCurrentChange(newPage) {
      this.orderParams.pagenum = newPage;
      this.getOrderList();
    },
    editDialogClose() {
      this.$$refs.editAddressFormRef.resetFields();
    },
    // 查看物流
    async getOrderTranMessage() {
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`);
      if (res.meta.status != 200)
        return this.$messgae.error("物流信息查询失败！");
      this.orderTranMessage = res.data;
      this.orderDialogVisible = true;
    },
  },
};
</script>

<style scoped lang="less">
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
</style>
